﻿(function ($) {

    $.extend(true, $.validator, {
        prototype: {
            addWrapper: function (toToggle) {
                //胡乱返回一个，避免为空异常又防止lable被显示出来。

                return $("<div />");
            }

        }
    });
}(jQuery));

(function ($) {
    function escapeAttributeValue(value) {
        // As mentioned on http://api.jquery.com/category/selectors/
        return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
    }

    function addErrorClass(element) {
        var group = element.closest('.form-group');
        if (group && group.length > 0) {
            group.addClass('has-error').removeClass('has-success');
        }
    }

    function addSuccessClass(element) {
        var group = element.closest('.form-group');
        if (group && group.length > 0) {
            group.addClass('has-success').removeClass('has-error');
        }
    }

    function onError(formElement, errorPlacementBase, error, inputElement) {
        errorPlacementBase(error, inputElement);

        if ($(inputElement).hasClass('input-validation-error')) {
            addErrorClass(inputElement)

            $(error).hide();

            var validation = $(inputElement).data("validation");
            var $popoverElement;
            if (validation) {
                $popoverElement = $(inputElement).parents(validation);

            } else {
                $popoverElement = $(inputElement);
            }
            $popoverElement.data("toggle", "popover").data("placement", "top")
                .data("content", $(error).text())
                .popover("show");
        }
    }

    function onSuccess(successBase, error, inputElement) {
        var container = error.data("unobtrusiveContainer");

        successBase(error);

        if (container) {
            addSuccessClass(container);
        }
        var validation = $(inputElement).data("validation");
        var $popoverElement;
        if (validation) {
            $popoverElement = $(inputElement).parents(validation);

        } else {
            $popoverElement = $(inputElement);
        }

        $popoverElement.popover("destroy");
    }

    $.fn.validateBootstrap = function (refresh) {
        return this.each(function () {
            var $this = $(this);
            if (refresh) {
                $this.removeData('validator');
                $this.removeData('unobtrusiveValidation');
                $.validator.unobtrusive.parse($this);
            }

            var validator = $this.data('validator');

            if (validator) {
                validator.settings.errorClass += ' text-danger';
                var errorPlacementBase = validator.settings.errorPlacement;
                var successBase = validator.settings.success;

                validator.settings.errorPlacement = function (error, inputElement) {
                    onError($this, errorPlacementBase, error, inputElement);
                };

                validator.settings.success = function (error, inputElement) {
                    onSuccess(successBase, error, inputElement);
                }

                $this.find('.input-validation-error').each(function () {
                    var errorElement = $this.find("[data-valmsg-for='" + escapeAttributeValue($(this)[0].name) + "']");
                    var newElement = $(document.createElement(validator.settings.errorElement))
                        .addClass('text-danger')
                        .attr('for', escapeAttributeValue($(this)[0].name))
                        .text(errorElement.text());
                    onError($this, errorPlacementBase, newElement, $(this));
                });
            }
            // if validation isn't enabled, but the form has the validation error message element, add error class to container
            else {
                $this.find('.input-validation-error').each(function () {
                    addErrorClass($(this));
                });
            }
        });
    };

    $(function () {
        $('form').validateBootstrap();
    });

}(jQuery));